package com.zhuangjie.mvc.validator.handler;

import com.zhuangjie.mvc.validator.annotate.PositiveNumber;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class PositiveNumberValidator implements ConstraintValidator<PositiveNumber, String> {
    @Override
    public void initialize(PositiveNumber constraintAnnotation) {
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        }
        try {
            return Double.parseDouble(value) > 0;
        } catch (NumberFormatException ex) {
            return false;
        }
    }
}